package com.linsir.lin.service.impl;

import com.linsir.lin.service.CamundaService;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.*;
import org.camunda.bpm.engine.impl.persistence.entity.DeploymentEntity;
import org.camunda.bpm.engine.impl.persistence.entity.ResourceEntity;
import org.camunda.bpm.engine.repository.Deployment;
import org.camunda.bpm.engine.repository.DeploymentQuery;
import org.camunda.bpm.engine.repository.ProcessDefinition;
import org.camunda.bpm.engine.repository.ProcessDefinitionQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName:linsir-lin
 * @Package:com.linsir.lin.service
 * @Title: CamundaServiceImpl
 * @Description: 流程相关服务时间
 * @Create:2021/6/27 17:30 by @author:Linsir
 * @Update:2021/6/27 17:30 by @author:Linsir
 * @UpdateRemark: 初始创建
 * @Version: 1.0.0
 */

@Service
@Slf4j
public class CamundaServiceImpl implements CamundaService {



    @Resource
    private ProcessEngine processEngine;

    @Resource
    private RepositoryService repositoryService;

    @Resource
    private RuntimeService runtimeService;

    @Resource
    private TaskService taskService;


    @Override
    public ProcessEngine get() {
        return processEngine;
    }


    /**
     *
     * 原理分析:
     * 他是一个懒加载对象,所以说,有一个作用域范围,在service层里面拿到数据库里面的session还在,但是到了controller已经不在了,
     * 而此时是@ResponseBody(JSON序列化) 在调用,一旦session不在,这个懒加载对象就加载不出来了
     *
     * 解决方法:
     * map其实就是实体类,既然是个实体类,那么就可以传入一个map
     *
     *
     */
    @Override
    public void deploymentList() {
        Deployment deployment =  repositoryService.createDeploymentQuery().deploymentId("1").singleResult();
        String source = deployment.getSource();
        log.info("xxxxxxxxxx:"+source+deployment.getDeploymentTime().toString());
        ProcessDefinition processDefinition = repositoryService.getProcessDefinition("P2:1:4");
        List<ProcessDefinition> processDefinitions = repositoryService.createProcessDefinitionQuery().list();
        processDefinitions.forEach(p->log.info(p.getId()+p.getResourceName()));
        log.info("xxxxxxxxxxxsxasdsd===="+processDefinition.getResourceName());
    }

    @Override
    public List<Map<String, String>> delopyLog() {
        List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
        List<Deployment> deploymentList =repositoryService.createDeploymentQuery().list();
        deploymentList.forEach( d->{
            DeploymentEntity deploymentEntity = (DeploymentEntity) d;
            Map<String, String> map = new HashMap<String, String>();
            map.put("id",deploymentEntity.getId());
            map.put("DeploymentTime",deploymentEntity.getDeploymentTime().toString());
            mapList.add(map);
        });
        return mapList;
    }

    @Override
    public List<ProcessDefinition> getProcessDefinitionList() {
        return repositoryService.createProcessDefinitionQuery().list();
    }

}
